package com.th.supcom.pds.outdrugreturnapply.server;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.carefx.framework.container.client.exception.UiException;
import net.carefx.framework.exception.BusinessException;
import net.carefx.framework.util.ConvertUtils;
import net.carefx.framework.web.gwt.server.GwtController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.common.client.model.PdsSupplyDetailModel;
import com.th.supcom.pds.common.client.model.PdsSupplyModel;
import com.th.supcom.pds.domain.vo.PatientInfo;
import com.th.supcom.pds.domain.vo.PdsSupplyDetailVO;
import com.th.supcom.pds.domain.vo.PdsSupplyResultSet;
import com.th.supcom.pds.domain.vo.PdsSupplyVO;
import com.th.supcom.pds.internal.api.OutDrugReturnApplyService;
import com.th.supcom.pds.internal.client.model.PatientInfoModel;
import com.th.supcom.pds.outdrugreturnapply.client.common.PdsSupplyResultModel;
import com.th.supcom.pds.outdrugreturnapply.client.service.OutDrugReturnApplyRpc;

/**
 * 门诊退药控制层.
 * 
 * @author yzhang
 */
public class OutDrugReturnServiceController extends GwtController implements OutDrugReturnApplyRpc
{
    private static final long serialVersionUID = 8165624832786257486L;
    private OutDrugReturnApplyService m_outDrugReturnApplyService;

    @Override
    public List <PdsSupplyResultModel> findOutDrugReturnInfo (Map <String, Object> conditionMap) throws UiException
    {
        List <PdsSupplyResultModel> pdsSupplyResultModels = new ArrayList <PdsSupplyResultModel> ();
        s_logger.debug ("开始通过输入条件查询退药信息,参数：conditionMap:{}", new Object[]
        { conditionMap });
        List <PdsSupplyResultSet> pdsSupplyResults = m_outDrugReturnApplyService.findOutDrugReturnInfo (conditionMap);
        if (pdsSupplyResults.isEmpty ())
        {
            s_logger.debug ("查询不到相关的退药信息！");
            throw new UiException ("查询不到相关的退药信息！");
        }
        for (int i = 0; i < pdsSupplyResults.size (); i++)
        {
            PdsSupplyResultModel pdsSupplyResultModel = new PdsSupplyResultModel ();
            pdsSupplyResultModel.setPdsSupplyModel ((PdsSupplyModel) ConvertUtils.convert (PdsSupplyModel.class, pdsSupplyResults.get (i).getPdsSupplyVO ()));
            List <PdsSupplyDetailModel> pdsSupplyDetailModels = ConvertUtils.convert (PdsSupplyDetailModel.class, pdsSupplyResults.get (i).getPdsSupplyDetailVOs ());
            pdsSupplyResultModel.setPdsSupplyDetailModels (pdsSupplyDetailModels);
            if (0 == i)
            {
                pdsSupplyResultModel.setPatientInfoModel ((PatientInfoModel) ConvertUtils.convert (PatientInfoModel.class, pdsSupplyResults.get (i).getPatientInfo ()));
            }
            pdsSupplyResultModels.add (pdsSupplyResultModel);
        }
        return pdsSupplyResultModels;
    }

    @Override
    public void doDrugReturn (PdsSupplyResultModel pdsSupplyResultModel) throws UiException
    {
        s_logger.debug ("进入doDrugReturn方法进行退药：入参【{}】", new Object[]
        { pdsSupplyResultModel });
        PdsSupplyResultSet pdsSupplyResultSet = new PdsSupplyResultSet ();
        PdsSupplyVO pdsSupplyVO = ConvertUtils.convert (PdsSupplyVO.class, pdsSupplyResultModel.getPdsSupplyModel ());
        List <PdsSupplyDetailVO> pdsSupplyDetailVOs = ConvertUtils.convert (PdsSupplyDetailVO.class, pdsSupplyResultModel.getPdsSupplyDetailModels ());
        PatientInfo patientInfo = ConvertUtils.convert (PatientInfo.class, pdsSupplyResultModel);
        pdsSupplyResultSet.setPdsSupplyVO (pdsSupplyVO);
        pdsSupplyResultSet.setPdsSupplyDetailVOs (pdsSupplyDetailVOs);
        pdsSupplyResultSet.setPatientInfo (patientInfo);
        pdsSupplyResultSet.setOperatorCode (pdsSupplyResultModel.getOperatorCode ());
        pdsSupplyResultSet.setOperatorName (pdsSupplyResultModel.getOperatorName ());
        try
        {
            m_outDrugReturnApplyService.saveDrugReturnInfo (pdsSupplyResultSet);
        }
        catch (Exception e)
        {
            throw new BusinessException (e.getMessage ());
        }

    }

    public void setOutDrugReturnApplyService (OutDrugReturnApplyService outDrugReturnApplyService)
    {
        m_outDrugReturnApplyService = outDrugReturnApplyService;
    }

    /**
     * 日志记录器.
     */
    public static final Logger s_logger = LoggerFactory.getLogger (OutDrugReturnServiceController.class);

}